#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>

void main()
{
	pid_t child;
	int status;

	printf("This is a demo how to get child status\n");

	if((child=fork()) == -1)
	{
		printf("fork error:%s\n",strerror(errno));
		exit(1);
	}
	else if (child == 0)
	{
		int i = 0;

		printf("I am the child:%ld\n",getpid());
		for(i=0;i<1000000;i++) sin(i);
		i=5;
		printf("I exit with %d\n",i);
		exit(i);
	}

	while(((child=wait(&status))==-1)&(errno==EINTR));
	if(child==-1)
		printf("Wait error:%s\n",strerror(errno));
		//printf("Wait result:111\n");
	else if(!status)
		printf("Child %ld terminated normally return status is zero!\n",child);
		//printf("Wait result:000\n");
	else if(WIFEXITED(status))
		printf("Child %ld terminated normally return status is %d\n",child,WEXITSTATUS(status));
		//printf("Wait result:222\n");
	else if(WIFSIGNALED(status))
		printf("Child %ld terminated due to signal %d znot caught\n",child,WTERMSIG(status));
		//printf("Wait result:333\n");
}
